let history = function () {
    this.initialize.apply(this, arguments);
};
history.prototype.initialize = function () {
    let canvas = Array.prototype.shift.call(arguments);
    this.canvas = canvas;
    this._history = [];
    this._redo = [];
    this.init();
};
history.prototype.add = function(obj){
    if(this._history.length>10){
        Array.prototype.shift.call(this._history);
    }
    Array.prototype.push.call(this._history,obj);
    this._redo = [];
}
history.prototype.init = function (){
    let that = this;
    if(this.canvas){
        this.canvas.on("add",function(obj){
            that.add(that.packoperation("add",obj))
        })
        this.canvas.on("remove",function(obj){
            that.add(that.packoperation("remove",obj))
        })
    }
}
history.prototype.packoperation = function(type,obj){
    let l = {
        "add":"remove",
        "remove":"add"}
    return {
        type:type,
        l:l[type],
        target:obj.target
    }
}
history.prototype.undo = function(){
    let obj = Array.prototype.pop.call(this._history);
    if(!obj){
        console.log("无法撤销");
        return false;
    }
    this.canvas[obj.l](obj.target);
    this._redo.push(obj)
}
history.prototype.do = function(){
    let obj = Array.prototype.pop.call(this._redo);
    if(!obj){
        console.log("无法重做");
        return false;
    }
    this.canvas[obj.type](obj.target);
    this._history.push(obj)
}
export default history;